<template>
  <div class="px-quarter-wrap">
    <px-popover v-model="visible" title="" content="" width="320">
      <template #reference>
        <px-input
          v-model="quarterDate"
          placeholder="请选择季度"
          clearable
          :prefix-icon="Calendar"
          readonly
          @click="visible = true"
          @change="quarterDateChange"
        >
          <template #suffix>
            <px-icon v-if="quarterDate" class="px-quarter-clear" @click="clearData">
              <Close />
            </px-icon>
          </template>
        </px-input>
      </template>

      <div class="px-quarter__header">
        <span class="px-quarter-btn px-quarter-btn__pre" @click="changeShowYear(-1)">
          <px-icon>
            <DArrowLeft />
          </px-icon>
        </span>
        <div class="px-quarter__header-text" @click="showYearList">
          {{ quarterTitle }}
        </div>

        <span class="px-quarter-btn px-quarter-btn__next" @click="changeShowYear(1)">
          <px-icon>
            <DArrowRight />
          </px-icon>
        </span>
      </div>

      <div v-if="!isEditYear" class="px-quarter__content">
        <div class="px-quarter__row">
          <span class="quarter-index" :class="{ 'is-active': showYear === pickerYear && quarterIndex === 1 }" @click="pickerQuarte(1)">第一季度</span>
          <span class="quarter-index" :class="{ 'is-active': showYear === pickerYear && quarterIndex === 2 }" @click="pickerQuarte(2)">第二季度</span>
        </div>
        <div class="px-quarter__row">
          <span class="quarter-index" :class="{ 'is-active': showYear === pickerYear && quarterIndex === 3 }" @click="pickerQuarte(3)">第三季度</span>
          <span class="quarter-index" :class="{ 'is-active': showYear === pickerYear && quarterIndex === 4 }" @click="pickerQuarte(4)">第四季度</span>
        </div>
      </div>

      <div v-else class="px-year__content">
        <div v-for="(item, index) in yearList" :key="index" class="px-year-item">
          <div class="cell" :class="{ 'is-active': showYear == item }" @click="selectYear(item)">
            {{ item }}
          </div>
        </div>
      </div>
    </px-popover>
  </div>
</template>
<script lang="ts" setup>
import { DArrowLeft, DArrowRight, Close, Calendar } from "@px-ui/icons-vue";

import { computed, onMounted, reactive, ref } from "vue";
let visible = ref(false);

const props = defineProps({
  modelValue: {
    type: String,
    default: ""
  }
});

const emits = defineEmits(["update:modelValue", "change"]);

// 绑定日期
let quarterDate = ref("");
// 选择的年
let pickerYear = ref("") as any;
// 展示的年
let showYear = ref("") as any;
// 选择的季度
let quarterIndex = ref(0);

// 是否展示年份列表
let isEditYear = ref(false);

// 年份列表开始年份
let startYear = ref("") as any;

// 年份列表
let yearList = reactive([] as any);

const quarterTitle = computed(() => {
  if (isEditYear.value) {
    return startYear.value + "年 - " + (startYear.value + 9) + "年";
  } else {
    return showYear.value + "年";
  }
});

// 选择某季度
function pickerQuarte(index: number) {
  quarterIndex.value = index;
  pickerYear.value = showYear.value;

  let oldValue = quarterDate.value; // 记录上一次数据
  quarterDate.value = pickerYear.value + "-第" + index + "季度";
  emits("update:modelValue", quarterDate.value);
  // 新老数据不一致，触发change时间
  if (oldValue !== quarterDate.value) {
    emits("change", quarterDate.value);
  }
}

// 更改展示的年
function changeShowYear(num: number) {
  if (isEditYear.value) {
    startYear.value = startYear.value + num * 10;
    // console.log('startYear.value', startYear.value)
    changeYearList();
  } else {
    showYear.value = showYear.value + num;
  }
}

// 清空选择的数据
function clearData() {
  quarterDate.value = "";
  pickerYear.value = "";
  showYear.value = new Date().getFullYear();
  quarterIndex.value = 0;
}

// 选择的数据
function quarterDateChange(value: any) {
  const splitArray = value.split("-Q");
  if (splitArray.length < 2) {
    pickerYear.value = "";
    showYear.value = new Date().getFullYear();
    quarterIndex.value = 0;
  } else {
    pickerYear.value = splitArray[0];
    showYear.value = splitArray[0];
    quarterIndex.value = splitArray[1];
  }
}

// 更改年份列表函数
function changeYearList() {
  yearList = [];
  let year = startYear.value;
  for (let i = 0; i < 10; i++) {
    yearList.push(year++);
  }
}

// 切换展示年份列表 和 季度
function showYearList() {
  if (!isEditYear.value) {
    startYear.value = Number(Math.floor(showYear.value / 10) + "0");
    changeYearList();
    isEditYear.value = true;
  } else {
    isEditYear.value = false;
  }
}

// 选中某个年份列表
function selectYear(item: any) {
  showYear.value = item;
  isEditYear.value = false;
}

onMounted(() => {
  // 初始化展示的年为当前年份
  showYear.value = new Date().getFullYear();
  startYear.value = Number(Math.floor(showYear.value / 10) + "0");
  changeYearList();
  // pickerYear.value = showYear.value
  // quarterIndex.value = 1;
  // quarterDate.value = `${ pickerYear.value}-第${quarterIndex.value}季度`;
  // emits("update:modelValue", quarterDate.value);
});
</script>
<style lang="scss" scoped>
.px-quarter__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;

  .px-quarter-btn {
    font-size: 12px;
  }

  .px-quarter__header-text {
    font-size: 16px;
    font-weight: 500;
    text-align: center;
    cursor: pointer;
  }
}

.px-quarter__content {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  min-height: 100px;

  .px-quarter__row {
    display: flex;
    justify-content: space-around;

    .quarter-index {
      display: flex;
      width: fit-content;
      padding: 4px 10px;
      cursor: pointer;

      &:hover {
        color: #337ecc;
      }
    }

    .is-active {
      color: #409eff;
    }
  }
}

.px-quarter-clear {
  position: relative;
  display: none;
  width: 12px;
  height: 12px;
  color: #909399;
  cursor: pointer;

  &::after {
    position: absolute;
    width: 14px;
    height: 14px;
    margin: auto;
    content: "";
    border: 1px solid #909399;
    border-radius: 50%;
  }
}

.px-input {
  &:hover {
    .px-quarter-clear {
      display: flex;
    }
  }
}

.px-year__content {
  display: flex;
  flex-wrap: wrap;
  min-height: 100px;
  padding: 10px 0;

  .px-year-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: calc(100% / 4);

    .cell {
      width: fit-content;
      padding: 4px 10px;
      white-space: nowrap;
      cursor: pointer;

      &:hover {
        color: #337ecc;
      }
    }

    .is-active {
      color: #409eff;
    }
  }
}
</style>
